package com.kycxika.app.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.kycxika.app.R;
import com.kycxika.app.activity.base.BaseActivity;
import com.kycxika.app.bean.RegisterBean;
import com.kycxika.app.dbutils.IntImp.KycXkIApi;
import com.kycxika.app.dbutils.inter.HttpQuestResultListener;

public class RegisActivity extends BaseActivity implements HttpQuestResultListener {


    private View mTop;
    private EditText family_username;
    private EditText family_pwd;
    private EditText family_againpwd;
    private KycXkIApi xkIApi;

    @Override
    protected void initView() {
        mTop = findViewById(R.id.top);
        setTop(mTop);
        getTitle("注册");
        getVisibility(true,this);


        family_username = (EditText) findViewById(R.id.family_username);
        family_pwd = (EditText) findViewById(R.id.family_pwd);
        family_againpwd = (EditText) findViewById(R.id.family_againpwd);

        xkIApi = KycXkIApi.getInstance(this);
        xkIApi.setHttpQuestResultListener(this);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {
        findViewById(R.id.regin).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!TextUtils.isEmpty(family_username.getText().toString())) {
                    if(!TextUtils.isEmpty(family_pwd.getText().toString())) {
                        if(!TextUtils.isEmpty(family_againpwd.getText().toString())) {
                            if(family_pwd.getText().toString().equals(family_againpwd.getText().toString())) {
                                xkIApi.getUserRegister(family_username.getText().toString(),family_pwd.getText().toString());
                            }else {
                                family_againpwd.setError("两次密码不一致.");
                            }
                        }else {
                            family_againpwd.setError("重复密码不能为空.");
                        }
                    }else {
                        family_pwd.setError("密码不能为空.");
                    }
                }else {
                    family_username.setError("用户名不能为空.");
                }
            }
        });
    }

    @Override
    public int getLayout() {
        return R.layout.activity_regis;
    }

    @Override
    public void onError(Object e) {
        if(e instanceof RegisterBean) {
            Toast.makeText(this,"网络开小差了.",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onSuccess(Object object) {
        if(object instanceof RegisterBean) {
            RegisterBean authorizedBean = (RegisterBean) object;
            Log.i("TAG","msg ----- " + authorizedBean.getMsg());
            if(authorizedBean!= null ) {
                Toast.makeText(this, authorizedBean.getMsg(), Toast.LENGTH_SHORT).show();
                startActivity(new Intent(this,LoginActivity.class));
                this.finish();
            }
        }
    }

    @Override
    public void onQuestErroCode(Object object) {
        if(object instanceof RegisterBean) {
            RegisterBean authorizedBean = (RegisterBean) object;
            Toast.makeText(this, authorizedBean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }
}
